package cn.wk.service.impl;

import cn.wk.client.LoginUserClient;
import cn.wk.domain.Employee;
import cn.wk.domain.LoginUser;
import cn.wk.domain.Tenant;
import cn.wk.domain.TenantType;
import cn.wk.dto.TenantEnteringDto;
import cn.wk.mapper.EmployeeMapper;
import cn.wk.mapper.TenantMapper;
import cn.wk.query.TenantQuery;
import cn.wk.service.ITenantService;
import cn.wk.util.AjaxResult;
import cn.wk.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2021-03-02
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    /**
     * 查询所有租户，并分页
     * @param query
     * @return
     */
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public PageList<Tenant> getTenantList(TenantQuery query) {
        Page<Tenant> page = new Page<>(query.getPage(), query.getRows());
        List<Tenant> datas = tenantMapper.getTenantList(page,query);
        return new PageList<Tenant>(page.getTotal(),datas);
    }

    /**
     * 添加机构
     * @param tenant
     */
    @Transactional
    @Override
    public void saveTenant(Tenant tenant) {
        tenantMapper.saveTenant(tenant);
    }

    /**
     * 修改机构
     * @param tenant
     */
    @Transactional
    @Override
    public void updateTenant(Tenant tenant) {
        tenant.setRegisterTime(null);
        tenantMapper.updateTenant(tenant);
    }

    /**
     * 机构入驻
     * @param dto
     */
    @Override
    public void entering(TenantEnteringDto dto) {
        //emp表中需要login_id,tenant_id
        //先去获得login_id，这里是属于其他服务，用feign调用
        //准备login
        LoginUser loginUser = createLoginUser(dto);
        //feign调用然后获取其自增id
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        if(ajaxResult.isSuccess()){
            Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
            //创建emp对象
            Employee empLoyee = createEmpLoyee(dto, loginId);
            //保存emp表,得到admin_id
            employeeMapper.insert(empLoyee);
            //创建tenant对象
            Tenant tenant = createTenant(dto,empLoyee.getId());
            //再保存tenant表
            tenantMapper.insert(tenant);
            //更新emp表中tenant_id
            empLoyee.setTenantId(tenant.getId());
            employeeMapper.updateById(empLoyee);
        }
    }

    /**
     * 创建tenant对象
     * @param dto
     * @return
     */
    private Tenant createTenant(TenantEnteringDto dto,Long adminId) {
        Tenant tenant = dto.getTenant();
        tenant.setAdminId(adminId);
        tenant.setRegisterTime(new Date());
        return tenant;
    }

    /**
     * 准备employee对象
     */
    private Employee createEmpLoyee(TenantEnteringDto dto,Long loginId) {
        Employee employee = dto.getEmployee();
        employee.setLoginId(loginId);
        employee.setRealName(employee.getUsername());
        employee.setState(0);
        employee.setType(5);
        return employee;
    }

    /**
     * 准备login对象
     */
    public LoginUser createLoginUser(TenantEnteringDto dto){
        LoginUser loginUser = new LoginUser();
        loginUser.setMealId(dto.getMealId());
        loginUser.setPassword(dto.getEmployee().getPassword());
        loginUser.setUsername(dto.getEmployee().getUsername());
        loginUser.setType(0);
        return loginUser;
    }


}
